<?php 

namespace app\seller\event;

use app\common\model\AreaRegion;
use app\common\model\ShippingArea as ShippingAreaModel;
use app\common\model\ShippingArea;
use app\common\utils\JsonService;

class ShippingAreaEvent{

    /*
    * 物流配置列表
    */
    public static function shippingAreaList($params){
        //$params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where[] = ['shop_id','=',$params['shop_id']];
        if($shipping_area_id = isset($params['shipping_area_id'])){
                    $where[] = ['shipping_area_id','=',$params['shipping_area_id']];
            }

        if($shipping_id = isset($params['shipping_id'])){
            $where[] = ['shipping_id','=',$params['shipping_id']];
        }

            if($shipping_area_name = isset($params['shipping_area_name'])){
                    $where[] = ['shipping_area_name','like',"%{$params['shipping_area_name']}%"];
            }
            if($shipping_code = isset($params['shipping_code'])){
                    $where[] = ['shipping_code','like',"%{$params['shipping_code']}%"];
            }
            if($shop_id = isset($params['shop_id'])){
                    $where[] = ['shop_id','like',"%{$params['shop_id']}%"];
            }
             $condition = [
            'where' => $where,
            'order' => ['shipping_area_id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
                'list_rows'=>20,
            ],
        ];
        $shippingArea = ShippingAreaModel::getPageAll($condition);
        return $shippingArea;
    }


    /*
    * 新增物流配置
    */
    public static function createShippingArea($data){
        $data['config'] = serialize($data['config']);
        $area_list = $data['area_list'];
        if(empty($data['area_list'])){
            return JsonService::error('请选择配送区域');
        }

        try {
            $res = ShippingAreaModel::create($data);
            $area = [];
            foreach($data['area_list'] as $k=>$v){
                $area[] = [
                    'shipping_area_id'=>$res->shipping_area_id,
                    'shop_id'=>$data['shop_id'],
                    'region_id'=>$v,
                ];
            }
            AreaRegion::setAll($area);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新物流配置
    */
    public static function updateShippingArea($data)
    {
        $data['config'] = serialize($data['config']);

        $model = ShippingArea::findModel($data['shipping_area_id'], []);
        if ($model->is_default == 0) {
            if (empty($data['area_list'])) {
                return JsonService::error('请选择配送区域');
            }
        }
        try {
            ShippingAreaModel::updateOne($data, ['shipping_area_id' => $data['shipping_area_id']]);
            if ($model->is_default == 0) {
                foreach ($data['area_list'] as $k => $v) {
                    $area[] = [
                        'shipping_area_id' => $data['shipping_area_id'],
                        'shop_id' => $data['shop_id'],
                        'region_id' => $v,
                    ];
                }

                AreaRegion::delByWhere(['shipping_area_id' => $data['shipping_area_id'], 'shop_id' => $data['shop_id']]);
                AreaRegion::setAll($area);
            }
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

